package com.yanqu.road.server.cmd.yrank.union;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UnionYRank;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manager.peakstronghold.CrossPeakStrongholdMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_LIST, desc = "")
public class CrossYRankGetUnionRankListCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        Integer conditionType = Integer.valueOf(reqMsg.getIdKey());

        if (conditionType == eGamePlayerEventType.BiaoJuUnionExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.sendUnionRankList(packet.getPlayerId(), serverId);
            }
            return;
        }

        List<UnionYRank> rankList = CrossYRankMgr.getSortUnionRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());
        int rankNum = CrossYRankMgr.getUnionRankNum(rankList, packet.getPlayerId());
        UnionYRank unionYRank = CrossYRankMgr.getUnionYRank(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey(), packet.getPlayerId());
        String reqUnionUid = unionYRank == null ? "" : unionYRank.getUnionUid();

        if (conditionType == eGamePlayerEventType.UnionExpRiseRank.getValue()) {
            CrossActivityProto.CrossActivityUnionRankListMsg.Builder rankBuilder = CrossActivityProto.CrossActivityUnionRankListMsg.newBuilder();
            rankBuilder.setActivityId(activityId);
            for (UnionYRank rank : rankList) {
                rankBuilder.addRankList(CrossActivityProto.CrossActivityUnionRankTempMsg.newBuilder()
                        .setUnionUid(rank.getUnionUid())
                        .setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(rank.getUnionBaseInfo()))
                        .setValue(rank.getValue().toString())
                );
            }

            CrossActivityProto.CrossActivityUnionRankListRespMsg.Builder builder = CrossActivityProto.CrossActivityUnionRankListRespMsg.newBuilder();
            builder.setActivityId(activityId);
            builder.setMyRank(rankNum);
            builder.setValue(unionYRank == null ? "0" : unionYRank.getValue().toString());
            builder.setRankList(rankBuilder);

            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_ACTIVITY_UNION_RANK_LIST, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        } else if (conditionType == eGamePlayerEventType.PeakSeacraftUnionRank.getValue()) {
            StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
            PeakSeacraftMgr.parseUnionRankMsgByUserId(activityId, packet.getPlayerId(), msg);
            YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GENERAL_ACTIVITY_UNION_RANK_LIST, msg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        } else if (conditionType == eGamePlayerEventType.PeakStrongHoldUnionIntegralRank.getValue() && CrossPeakStrongholdMgr.isInBattle(activityId, reqMsg.getGroupId())) {
            UnionActivityRankProto.UnionActivityRankListMsg rankBuilder = CrossPeakStrongholdMgr.getNowScoreUnionRankListMsg(activityId, reqMsg.getGroupId());
            int rank = 0;
            int myRank = -1;
            String myValue = "0";
            if (!StringUtils.isNullOrEmpty(reqUnionUid)) {
                for (UnionActivityRankProto.UnionActivityRankTempMsg unionActivityRankTempMsg : rankBuilder.getRankListList()) {
                    rank++;
                    if (unionActivityRankTempMsg.getUnionUid().equals(reqUnionUid)) {
                        myRank = rank;
                        myValue = unionActivityRankTempMsg.getValue();
                        break;
                    }
                }
            }
            StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder builder = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
            builder.setMyRank(myRank);
            builder.setValue(myValue);
            builder.setRankList(rankBuilder);
            YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GENERAL_ACTIVITY_UNION_RANK_LIST, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }

        UnionActivityRankProto.UnionActivityRankListMsg.Builder rankBuilder = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
        for (UnionYRank rank : rankList) {
            UnionActivityRankProto.UnionActivityRankTempMsg.Builder rankTemp = UnionActivityRankProto.UnionActivityRankTempMsg.newBuilder()
                    .setUnionUid(rank.getUnionUid())
                    .setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(rank.getUnionBaseInfo()))
                    .setValue(rank.getValue().toString());
            if (!StringUtils.isNullOrEmpty(rank.getParam())) {
                rankTemp.setParamValue(rank.getParam());
            }
            rankBuilder.addRankList(rankTemp);
        }

        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder builder = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
        builder.setMyRank(rankNum);
        builder.setValue(unionYRank == null ? "0" : unionYRank.getValue().toString());
        builder.setRankList(rankBuilder);
        builder.setParam(unionYRank == null ? "0" : unionYRank.getParam());

        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        int actvityType = 0;
        if(null != activityInfo){
            actvityType = activityInfo.getType();
        }
        if (actvityType == eActivityType.OreWarActivity.getValue()){
            OreWarActivityProto.OreWarActivityUnionRankListRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityUnionRankListRespMsg.newBuilder();
            respMsg.setMyRank(rankNum);
            respMsg.setRankList(rankBuilder);
            respMsg.setValue(unionYRank == null ? "0" : unionYRank.getValue().toString());
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ORE_WAR_ACTIVITY_UNION_RANK, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }else if (actvityType == eActivityType.CrossTradeWarActivity.getValue()){
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_TRADE_WAR_ACTIVITY_UNION_RANK, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }

        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GENERAL_ACTIVITY_UNION_RANK_LIST, builder);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
